#include "stdafx.h"
#include "buffer.h"

//argb_32 argb32(uint_32 color)
//{
//	argb_32 val;
//	val.color = color;
//	return val;
//}

/************************************* core_buffer *******************************/
buffer * buffer_create(uint_x width, uint_x height, uint_x strike)
{
	//buffer * p_buffer = new buffer();
	buffer * p_buffer = (buffer *)drawing_malloc(sizeof(buffer));
	p_buffer->pitch = strike;
	p_buffer->width = width;
	p_buffer->height = height;


	//p_buffer->bits = new uint_8[p_buffer->strike * p_buffer->height];
	p_buffer->bits = (uint_8 *)drawing_malloc(p_buffer->pitch * p_buffer->height);
	return p_buffer;
}
void  buffer_destroy(buffer * buffer)
{
	//delete[] buffer->bits;
	//delete buffer;

	drawing_free(buffer->bits);
	drawing_free(buffer);
}

buffer * buffer_attach(uint_8 * bits, uint_x width, uint_x height, uint_x strike)
{
	//buffer * p_buffer = new buffer();
	buffer * p_buffer = (buffer *)drawing_malloc(sizeof(buffer));
	p_buffer->pitch = strike;
	p_buffer->width = width;
	p_buffer->height = height;

	p_buffer->bits = bits;
	return p_buffer;
}

void buffer_detach(buffer * buffer)
{
	//delete buffer;
	drawing_free(buffer);
}

uint_x buffer_get_size(buffer * buffer)
{
	return buffer->pitch * buffer->height;
}

int_32 * buffer_get_line_argb32(buffer * buffer, uint_x line)
{
	return (int_32 *)(buffer->bits + buffer->pitch * line);
}

void buffer_clear_argb32(buffer * buffer, int_32 val)
{
	for(uint_x cnt = 0, size = buffer->height; cnt != size; ++cnt)
		buffer_line_clear_argb32(buffer, cnt, val);
}

void buffer_clear_direct(buffer * buffer, uint_8 val)
{
	uint_x size = buffer->pitch * buffer->height;
	drawing_memset(buffer->bits, val, size);
}
void buffer_line_clear_argb32(buffer * buffer, uint_x line, int_32 val)
{
	int_32 * pl = buffer_get_line_argb32(buffer, line);

	for(uint_x cnt = 0, size = buffer->width; cnt != size; ++cnt)
		*pl++ = val;
}




rgb24 * buffer_get_line_rgb24(buffer * buffer, uint_x line)
{
	return (rgb24 *)(buffer->bits + buffer->pitch * line);
}
void    buffer_clear_rgb24(buffer * buffer, rgb24 val)
{
	for(uint_x cnt = 0, size = buffer->height; cnt != size; ++cnt)
		buffer_line_clear_rgb24(buffer, cnt, val);
}
void    buffer_line_clear_rgb24(buffer * buffer, uint_x line, rgb24 val)
{
	rgb24 * pl = buffer_get_line_rgb24(buffer, line);

	for(uint_x cnt = 0, size = buffer->width; cnt != size; ++cnt)
		*pl++ = val;
}



rgb16 * buffer_get_line_rgb16(buffer * buffer, uint_x line)
{
	return (rgb16 *)(buffer->bits + buffer->pitch * line);
}
void   buffer_clear_rgb16(buffer * buffer, rgb16 val)
{
	for(uint_x cnt = 0, size = buffer->height; cnt != size; ++cnt)
		buffer_line_clear_rgb16(buffer, cnt, val);
}
void    buffer_line_clear_rgb16(buffer * buffer, uint_x line, rgb16 val)
{
	rgb16 * pl = buffer_get_line_rgb16(buffer, line);

	for(uint_x cnt = 0, size = buffer->width; cnt != size; ++cnt)
		*pl++ = val;
}











uint_x util_calc_buffer_line_size(uint_x width, uint_x align)
{
	uint_x size = width;
	switch(align)
	{
	case 1:
		size >>= 3;
		if(width & 7)
			++size;
		break;
	case 4:
		size >>= 1;
		if(width & 3)
			++size;
		break;
	case 8:
		break;
	case 16:
		size <<= 1;
		break;
	case 24:
		size *= 3;
		break;
	case 32:
		size <<= 2;
		break;
	case 48:
		size *= 6;
		break;
	case 64:
		size <<= 3;
		break;
	default:
		size = 0;
		break;
	}
	return ((size + 3) >> 2) << 2;
}